﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace MsTestUtilities.SolutionAnalysis
{
    public class SolutionFile
    {      
        private List<CsProject> projects;
        public IEnumerable<CsProject> Projects { get { return projects; } }

        public SolutionFile()
        {
        }

        public SolutionFile(IEnumerable<CsProject> projects)
        {
            this.projects = projects.ToList();
        }

        public static SolutionFile LoadFromInfo(SolutionInfo info)
        {
            if (info.Path == null)
            {
                throw new ArgumentException("Solution path was not set. Cannot load solution", "info");
            }

            var projects = new List<CsProject>();
            foreach(var projectInfo in info.Entries.Where(e => e.Type == EntryType.Project))
            {
                var projectPath = Path.Combine(info.Directory, projectInfo.RelativePath);
                var project = CsProject.LoadFromFile(projectPath);
                projects.Add(project);
            }

            return new SolutionFile(projects);
        }
    }
}
